<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="到期时间" prop="expireTime">
        <el-date-picker
          v-model="queryParams.expireTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"> <Icon icon="ep:search" class="mr-5px" /> 搜索 </el-button>
        <el-button @click="resetQuery"> <Icon icon="ep:refresh" class="mr-5px" /> 重置 </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户信息" align="center" min-width="100px">
        <template #default="scope">
          <el-avatar :src="scope.row.user?.avatar || './avatar.png'" /><br />
          <el-tooltip :content="'用户编号：' + scope.row.userId" placement="top">
            <el-text>{{ scope.row.user?.nickname }}</el-text>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="推广码" align="center" width="120px">
        <template #default="scope">
          <el-image
            fit="cover"
            title="[推广码]"
            :src="scope.row.qrcode || '/placeholder.png'"
            :preview-src-list="[scope.row.qrcode || './placeholder.png']"
            :initial-index="0"
            :preview-teleported="true"
            :hide-on-click-modal="true"
            class="flex-none w-50px h-50px ml-5px cursor-pointer"
          />
        </template>
      </el-table-column>
      <el-table-column label="级别" align="center" min-width="80px">
        <template #default="scope">
          <el-tag type="primary">{{ scope.row.levelName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="统计购买信息" align="left" min-width="120px">
        <template #default="scope">
          <div>累计金额：¥{{ fenToYuan(scope.row.cumulativePrice) }}</div>
          <div>累计次数：{{ scope.row.purchaseCount }}</div>
          <div>累计天数：{{ scope.row.daysCount }}</div>
          <div>最近天数：{{ scope.row.lastDays }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="valid" width="60">
        <template #default="scope">
          <el-text :type="scope.row.valid ? 'success' : 'danger'">{{
            scope.row.valid ? '有效' : '失效'
          }}</el-text>
        </template>
      </el-table-column>
      <el-table-column
        label="到期时间"
        align="center"
        prop="expireTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { fenToYuan } from '@/utils'
import { AgencyApi } from '@/api/member/agency/user'

/** 用户信息 列表 */
defineOptions({ name: 'AgencyUser' })

const loading = ref(true) // 列表的加载中
const list = ref<any>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  expireTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AgencyApi.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
